package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
)

// ... 其他代码 ...

var price map[string]int

// CalculateHandler 处理计价请求
func CalculateHandler(c *gin.Context) {
	file1, _, err := c.Request.FormFile("file1")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "账号清单上传失败"})
		return
	}
	all, err := ioutil.ReadAll(file1)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "账号清单上传失败"})
		return
	}
	ansi, err := convertToANSI(all)
	trim := strings.Split(ansi, "\n")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "自定义定价策略上传失败"})
		return
	}
	// 遍历账号表单
	for i := range trim {
		s := trim[i]
		fmt.Println(s)
		// 获取每一条账号的角色信息列表
		totalPrice := 0
		if strings.Index(s, "【") != 0 {
			characters := strings.Split(s[strings.Index(s, "【")+3:strings.Index(s, "】")], "|")
			fmt.Println("正在计算该账号价格", characters)
			// 获取每一个角色

			for k := range characters {
				character := characters[k]
				characterName := ""
				characterLevel := 0
				characterPrice := 0
				if strings.Index(character, "*") != -1 {
					// 多命
					characterName = character[:strings.Index(character, "*")]
					characterLevel, _ = strconv.Atoi(character[strings.Index(character, "*")+1:])
					if p, ok := price[characterName]; ok {
						characterPrice = (2 * p) * characterLevel
					} else {
						fmt.Println("缺少人物" + characterName)
					}
				} else {
					characterName = character
					if p, ok := price[characterName]; ok {
						characterPrice = p
					} else {
						fmt.Println("缺少人物" + characterName)
					}
				}
				fmt.Printf("人物%s  命座 %d 价格是 %d \n", characterName, characterLevel, characterPrice)
				totalPrice += characterPrice
			}
		}

		if strings.Index(s, "《") != -1 {
			fmt.Printf("正在计算武器价格")
			weapons := strings.Split(s[strings.Index(s, "《")+3:strings.Index(s, "》")], "|")
			fmt.Println("正在计算该武器价格", weapons)
			// 获取每一个武器
			for k := range weapons {
				weapon := weapons[k]
				weaponName := ""
				weaponLevel := 0
				weaponPrice := 0
				if strings.Index(weapon, "*") != -1 {
					// 多命
					weaponName = weapon[:strings.Index(weapon, "*")]
					weaponLevel, _ = strconv.Atoi(weapon[strings.Index(weapon, "*")+1:])
					if p, ok := price[weaponName]; ok {
						weaponPrice = (2 * p) * weaponLevel
					} else {
						fmt.Println("缺少武器" + weaponName)
					}
				} else {
					weaponName = weapon
					if p, ok := price[weaponName]; ok {
						weaponPrice = p
					} else {
						fmt.Println("缺少人物" + weaponName)
					}
				}
				fmt.Printf("武器:%s 等级%d 价格是%d \n", weaponName, weaponLevel, weaponPrice)
				totalPrice += weaponPrice
			}
		}

	}
	// 构建结果页面的数据
	data := map[string]interface{}{
		"Price": 0,
	}

	// 返回结果页面
	c.HTML(http.StatusOK, "result.html", data)
}

func main() {
	// 创建Gin引擎
	r := gin.Default()
	price = resolvePrice()
	// 设置HTML模板路径
	r.LoadHTMLGlob("templates/*")

	// 设置路由
	r.GET("/", IndexHandler)
	r.GET("/index", IndexHandler)
	r.GET("/item-definition", ItemDefinitionHandler)

	r.POST("/calculate", CalculateHandler)

	// 启动服务器
	r.Run(":8080")
}

// IndexHandler 处理首页请求
func IndexHandler(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", nil)
}

// ItemDefinitionHandler 处理角色请求
func ItemDefinitionHandler(c *gin.Context) {
	c.HTML(http.StatusOK, "item-definition.html", nil)
}

// 多UP计价
func totalPrice(price, count int) int {
	return price * 2
}
